<?php
require_once "xpface/util/XPFaceConfig.class.php";
require_once "xpface/util/PageConfig.class.php";
require_once "xpface/util/DataSourceConfig.class.php";
class XPFaceConfigLoader
{
    private static $_loader = null;
	private $_xpFaceConfig = array();
    private $_pageConfig = array();
    private $_dataSourceConfig = array();
    
    public static function getInstance() {
        if (self::$_loader instanceof self) {
            return self::$_loader;
        }
        self::$_loader = new XPFaceConfigLoader();
        self::$_loader->load(XPFACE_CONFIG);
        return self::$_loader;
    }

	public function load($filename)
	{
        //xpface config
        $xml = simplexml_load_file($filename);
        //var_dump($xml->children());
        foreach ($xml->config as $configNode) {
            $xpFaceConfig = new XPFaceConfig();
            $xpFaceConfig->key = (string) $configNode["key"];
            $xpFaceConfig->value = (string) $configNode["value"];
            $this->_xpFaceConfig[$xpFaceConfig->key] = $xpFaceConfig;
        }
        //page config
        $this->setPageConfig($this->_xpFaceConfig["pages_config_xml_file"]->value);
        //Data Source config
        $this->setDataSourceConfig($this->_xpFaceConfig["data_source_xml_file"]->value);
    }
    public function setDataSourceConfig($dataSourceConfigFile) {
        if (file_exists($dataSourceConfigFile)) {
            $xml = simplexml_load_file($dataSourceConfigFile);
            $this->loopSetDataSourceConfig($xml->uimodule);
        } else {
            $configFileDir = dirname($dataSourceConfigFile);
            $tmpFileNames = scandir($configFileDir);
            foreach ($tmpFileNames as $tmpFileName) {
                if (strpos($tmpFileName, "data_source.xml") === false) {
                    continue;
                }
                $xml = simplexml_load_file($configFileDir . "/" . $tmpFileName);
                $this->loopSetDataSourceConfig($xml->uimodule);
            }
        }
    }
    
    public function loopSetDataSourceConfig($uimodules) {
        foreach ($uimodules as $uimoduleNode) {
            $xpDataSourceConfig = new DataSourceConfig();
            $xpDataSourceConfig->uimodule = (string) $uimoduleNode["src"];
            if (array_key_exists(md5($xpDataSourceConfig->uimodule), $this->_dataSourceConfig)) {
                echo "Data Source Config \"{$xpDataSourceConfig->uimodule}\" is repeat.<br />";
                continue;
            }
            foreach ($uimoduleNode->data as $dataNode) {
                $xpDataSourceConfig->data[(string) $dataNode["key"]] = (string) $dataNode["source"];
            }
            $this->_dataSourceConfig[md5($xpDataSourceConfig->uimodule)] = $xpDataSourceConfig;
        }
    }
    
    public function setPageConfig($configFile) {
        $pages = array();
        if (file_exists($configFile)) {
            $xml = simplexml_load_file($configFile);
            $this->loopSetPageConfig($xml->page, (string)$xml["login"], (string)$xml["permission"]);
            //var_dump($xml);
        } else {
            $configFileDir = dirname($configFile);
            $tmpFileNames = scandir($configFileDir);
            foreach ($tmpFileNames as $tmpFileName) {
                if (strpos($tmpFileName, ".xml") === false) {
                    continue;
                }
                $xml = simplexml_load_file($configFileDir . "/" . $tmpFileName);
                $this->loopSetPageConfig($xml->page, (string)$xml["login"], (string)$xml["permission"]);
            }
        }
    }
    
    public function loopSetPageConfig($pages, $login, $permission) {
        if ($login == "") {
            $login = "no";
        }
        if ($permission == "") {
            $permission = "no";
        }
        foreach ($pages as $pageNode) {
            //var_dump($pageNode["id"]);
            $pageConfig = new PageConfig();
            $pageConfig->id = (string) $pageNode["id"];
            if ($pageConfig->id == "") {
                continue;
            }
            if (array_key_exists($pageConfig->id, $this->_pageConfig)) {
                echo "Page \"{$pageConfig->id}\" is repeat.<br />";
                continue;
            }
            //login
            $mustLogin = $login;
            //var_dump((string)$pageNode["login"]);
            if ((string)$pageNode["login"] != "") {
                $mustLogin = (string)$pageNode["login"];
            }
            //var_dump($mustLogin);
            $pageConfig->login = $mustLogin;
            
            //check permission
            $checkPermission = $permission;
            if ((string)$pageNode["permission"] != "") {
                $checkPermission = (string)$pageNode["permission"];
            }
            //var_dump($checkPermission);
            $pageConfig->permission = $checkPermission;
            
            //template
            $pageConfig->template = (string) $pageNode["template"];
            //regions
            foreach ($pageNode->region as $regionNode) {
                $pageConfig->regions[(string) $regionNode["id"]] = (string) $regionNode["uimodule"];
            }
            //binds
            foreach ($pageNode->bind as $bindNode) {
                $pageConfig->binds[(string) $bindNode["key"]] = (string) $bindNode["value"];
            }
            $this->_pageConfig[$pageConfig->id] = $pageConfig;
        }
    }
    
    public function getXPFaceConfig() {
        return $this->_xpFaceConfig;
    }
    public function getPageConfig() {
        return $this->_pageConfig;
    }
    public function getDataSourceConfig($uimoduleSrc) {
        return $this->_dataSourceConfig[md5($uimoduleSrc)];
    }
}
